package zd.cornermemory.ui;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import zd.cornermemory.App;
import zd.cornermemory.R;
import zd.cornermemory.adapter.PjCjAdapter;
import zd.cornermemory.db.Cj;
import zd.cornermemory.utils.Statistics;

import static zd.cornermemory.utils.Statistics.timeToString;

public class CjPjDetailActivity extends BaseActivity {
    private int      currentPos;
    private TextView zpj;
    private TextView min;
    private TextView max;
    private ListView listview;
    private List<Cj> cjs;
    private Cj minCj,maxCj;
    private PjCjAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cj_pj_detail);
        currentPos = getIntent().getExtras().getInt("currentPos");
        findViews();
        initData();
        setEvent();
        setBackImage();
    }

    private void setEvent() {
    }

    private void initData() {

        cjs = App.getInstance().getAllCjMap().get(currentPos);
        Collections.sort(cjs, new Comparator<Cj>() {
            @Override
            public int compare(Cj cj, Cj t1) {
                return (int)(cj.getId()-t1.getId());
            }
        });
        if (cjs == null) {
            cjs = new ArrayList<>();
        }
        mAdapter = new PjCjAdapter(mContext, cjs);
        listview.setAdapter(mAdapter);
        setMinMax();
    }

    private void findViews() {
        zpj = findViewById(R.id.zpj);
        min = findViewById(R.id.min);
        max = findViewById(R.id.max);
        listview = findViewById(R.id.listview);
    }

    private void setMinMax() {
        List<Cj> cjList = new ArrayList<>();
        for (Cj cj : cjs) {
            if (cj.getStatus() != 2) {
                cjList.add(cj);
            }
        }
        long avg = 0;
        for (Cj cj : cjList) {
            avg += cj.getSingTime();
        }
        if (avg == 0) {
            zpj.setText("N/A");
            min.setText("N/A");
            max.setText("N/A");
            minCj = null;
            maxCj = null;
        }else{
            double a = avg/cjList.size();
            avg = Math.round(a);
            String avgStr = timeToString((int) avg);
            zpj.setText(avgStr);
            minCj = Collections.min(cjList);
            maxCj = Collections.max(cjList);
            min.setText(Statistics.timeToString(minCj.getSingTime().intValue()));
            max.setText(Statistics.timeToString(maxCj.getSingTime().intValue()));

            long sum = 0;
            for (Cj c : cjList) {
                long time = c.getSingTime();
                sum += (time-avg)*(time-avg);
            }
            double s = sum/(cjList.size()-1<=0?1:cjList.size()-1);
            double standardDevition = Math.sqrt(s)/1000;
            DecimalFormat format = new DecimalFormat("######0.00");
            zpj.setText(avgStr+"(σ = "+format.format(standardDevition)+")");

        }
    }

}
